
import runSaga from './runSaga'
import createChannel from './createChannel'

export default function createSagaMiddleware(){
    let channel = createChannel() //创建一个监听器
    let boundRunSaga;
    function sagaMiddleware({getState,dispatch}){
        boundRunSaga=runSaga.bind(null,{channel,dispatch});
        return function (next){
            // 这个方法就是改造后的store.dispatch
            return function (action){
                const result = next(action); //先请求一次store.dispatch(action) 
                channel.emit(action) //订阅 action=async_add
                return result
            }
        }
    }
    sagaMiddleware.run = (saga)=>boundRunSaga(saga)
    return sagaMiddleware
 
} 